<?php
declare (strict_types = 1);

namespace app\admin\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class ChargePile extends Model
{
    protected $pk = "id";

    /**
     * 获取信息
     * @param $id
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id){
        return $this->where('id',$id)->find();
    }

    /**
     * 获取信息
     * @param $device_sn
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoByDeviceSn($device_sn){
        return $this->where('device_sn',$device_sn)->find();
    }

    /**
     * 列表
     * @param $getData
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getList($getData,$page,$limit){
        $where = ['is_delete'=>1];
        $sql = $this->where($where);
        if(isset($getData['title']) && $getData['title']) $sql = $sql->whereLike('title','%'.$getData['title'].'%');
        if(isset($getData['device_sn']) && $getData['device_sn']) $sql = $sql->whereLike('device_sn','%'.$getData['device_sn'].'%');
        if(isset($getData['station_id']) && $getData['station_id']) $sql = $sql->where('station_id',$getData['station_id']);
        if(isset($getData['is_status']) && $getData['is_status']) $sql = $sql->where('station_id',$getData['is_status']);
        $count = $sql->count();
        $statusType = [0=>'关闭',1=>'开启'];
        $typeType = [1=>'直流充电桩',2=>'交流充电桩'];
        $chargeType = [1=>'快充',2=>'慢充'];
        $stationModel = new ChargingStation();
        $stationList = $stationModel->column('title','id');
        $data = $sql->page($page,$limit)->order('id','desc')->select()->each(function ($item)use($statusType,$typeType,$stationList,$chargeType){
            $item['status_name'] = $statusType[$item['is_status']];
            $item['type_name'] = $typeType[$item['device_type']];
            $item['station_name'] = $stationList[$item['station_id']];
            $item['charge_name'] = $chargeType[$item['charge_type']];
            return $item;
        })->toArray();
        return compact('count','data');
    }

    /**
     *  添加数据
     * @param $addData
     * @return int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addData($addData){
        return $this->insertGetId([
            'title'=>$addData['title'],
            'device_sn'=>$addData['device_sn'],
            'station_id'=>$addData['station_id'],
            'number_sn'=>$addData['number_sn'],
            'device_type'=>$addData['device_type'],
            'charge_type'=>$addData['charge_type'],
            'power_number'=>$addData['power_number'],
            'voltage'=>isset($addData['voltage']) ? $addData['voltage'] : 0,
            'electric_current'=>isset($addData['electric_current']) ? $addData['electric_current'] : 0,
            'weight'=>isset($addData['weight']) ? $addData['weight'] : 0,
            'size'=>isset($addData['size']) ? $addData['size'] : '',
            'charge_cost'=>$addData['charge_cost'],
            'service_cost'=>$addData['service_cost'],
            'is_status'=>$addData['is_status'],
            'remark'=>$addData['remark'],
            'is_delete'=>1,
            'create_time'=>date("Y-m-d H:i:s"),
            'update_time'=>date("Y-m-d H:i:s"),
            'cost_id'=>$addData['cost_id'],
            'peak_electric_cost'=>$addData['peak_electric_cost'],
            'peak_service_cost'=>$addData['peak_service_cost'],
            'flat_electric_cost'=>$addData['flat_electric_cost'],
            'flat_service_cost'=>$addData['flat_service_cost'],
            'valley_electric_cost'=>$addData['valley_electric_cost'],
            'valley_service_cost'=>$addData['valley_service_cost'],
        ]);
    }

    /**
     * 修改
     * @param $saveData
     * @return static
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function editData($saveData){
        $updateData = [
            'title'=>$saveData['title'],
            'device_sn'=>$saveData['device_sn'],
            'station_id'=>$saveData['station_id'],
            'number_sn'=>$saveData['number_sn'],
            'device_type'=>$saveData['device_type'],
            'charge_type'=>$saveData['charge_type'],
            'power_number'=>$saveData['power_number'],
            'voltage'=>isset($saveData['voltage']) ? $saveData['voltage'] : 0,
            'electric_current'=>isset($saveData['electric_current']) ? $saveData['electric_current'] : 0,
            'weight'=>isset($saveData['weight']) ? $saveData['weight'] : 0,
            'size'=>isset($saveData['size']) ? $saveData['size'] : '',
            'charge_cost'=>$saveData['charge_cost'],
            'service_cost'=>$saveData['service_cost'],
            'is_status'=>$saveData['is_status'],
            'remark'=>$saveData['remark'],
            'update_time'=>date("Y-m-d H:i:s"),
            'cost_id'=>$saveData['cost_id'],
            'peak_electric_cost'=>$saveData['peak_electric_cost'],
            'peak_service_cost'=>$saveData['peak_service_cost'],
            'flat_electric_cost'=>$saveData['flat_electric_cost'],
            'flat_service_cost'=>$saveData['flat_service_cost'],
            'valley_electric_cost'=>$saveData['valley_electric_cost'],
            'valley_service_cost'=>$saveData['valley_service_cost'],
        ];
        return $this->where('id',$saveData['id'])->update($updateData);
    }

    /**
     * 删除
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function delData($id){
        $getInfo = $this->getInfoById($id);
        if($getInfo['is_delete'] == 0) return ['code'=>0,'msg'=>'删除成功'];
        $result = $this->where('id',$id)->update(['is_delete'=>0]);
        if($result) return ['code'=>0,'msg'=>'删除成功'];
        return ['code'=>1,'msg'=>'删除失败，请稍后重试'];
    }

    /**
     * 获取指定列表
     * @param $where
     * @param $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getAllList($where,$field='*'){
        return $this->where($where)->field($field)->select()->toArray();
    }

}
